package shuiyuqing.network.http;

import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xkh.xkh.MyApplication;
import managers.UIHandler;
import managers.VolleySingleton;
import shuiyuqing.activities.WaterActivity;
import shuiyuqing.network.beans.IrrBRMeteorological;
import shuiyuqing.network.beans.IrrBRRain;

/**
 * 获取雨量、气象信息
 * Created by 凯 on 2015/11/7.
 */
public class GetRainWeather {
    Download download;

    String stadiaCode;
    UIHandler.BaseActivity activity;

    public GetRainWeather(WaterActivity waterActivity, String stadiaCode) {
        activity = waterActivity;
        this.stadiaCode = stadiaCode;
    }

    public Download getDownload() {
        return download;
    }

    public GetRainWeather(String stadiaCode) {
        this.stadiaCode = stadiaCode;
    }

    // 获取雨晴气象信息
    public void get() {
        if (activity != null)
        {
            activity.showDialog();
        }

        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/guanqu/GetRainWeater";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (activity != null)
                        {
                            activity.closeDialog();
                        }
                        afterGet(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (activity != null)
                {
                    activity.closeDialog();
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("data", stadiaCode);
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    // 获取下载数据
    private void afterGet(String response) {

        download = JSON.parseObject(response, Download.class);
        if (download.getStatus() == DownloadInfo.SUCCESS) {
            Message message = Message.obtain();
            message.what = 0x52;
            message.obj = GetRainWeather.this;
            UIHandler.uiHandler.dispatchMessage(message);
        } else if (download.getStatus() == DownloadInfo.FAILED) {

        }
    }

    // 下行数据
    public static class Download {

        int status;
        List<IrrBRRain> brRains;
        List<IrrBRMeteorological> brMeteorologicals;


        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<IrrBRRain> getBrRains() {
            return brRains;
        }

        public void setBrRains(List<IrrBRRain> brRains) {
            this.brRains = brRains;
        }

        public List<IrrBRMeteorological> getBrMeteorologicals() {
            return brMeteorologicals;
        }

        public void setBrMeteorologicals(List<IrrBRMeteorological> brMeteorologicals) {
            this.brMeteorologicals = brMeteorologicals;
        }


    }

}
